package com.cmcc.aregister.ui.activity;

import com.cmcc.aregister.PageControlView;
import com.cmcc.aregister.R;
import com.cmcc.aregister.ScrollViewGallery;
import com.cmcc.aregister.ui.adapter.UserGuideGalleryAdapter;
import com.cmcc.sdk.CmccDataStatistics;

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;

public class UserGuideActivity extends Activity implements OnTouchListener,
    OnGestureListener
{
    private static final String TAG = "UserGuideActivity";
    private ScrollViewGallery scrollViewGallery;
    private UserGuideGalleryAdapter adapter;
    private GestureDetector detector;
    private PageControlView page_control;

    public UserGuideActivity()
    {
        detector = new GestureDetector(this);
    }

    private int page;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.user_guide_layout);
        scrollViewGallery = (ScrollViewGallery) findViewById(R.id.user_guide);
        page_control = (PageControlView) findViewById(R.id.pageControlView);
        page_control.count = 1;
        page_control.generatePageControl(0);
        adapter = new UserGuideGalleryAdapter(UserGuideActivity.this);
        scrollViewGallery
            .setOnItemSelectedListener(new OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3)
                {
                    Log.i(TAG, "滑动到第：" + arg2);
                    page_control.generatePageControl(arg2);
                    page = arg2;
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0)
                {

                }
            });
        scrollViewGallery.setOnTouchListener(this);
        scrollViewGallery.setAdapter(adapter);
    }

    @Override
    protected void onDestroy()
    {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @Override
    protected void onPause()
    {
        // TODO Auto-generated method stub
        super.onPause();
    }

    @Override
    protected void onResume()
    {
        // TODO Auto-generated method stub
        super.onResume();
        CmccDataStatistics.onStart(this);
    }

    @Override
    protected void onStop()
    {
        // TODO Auto-generated method stub
        super.onStop();
        CmccDataStatistics.onStop(this);
    }

    @Override
    public boolean onTouch(View arg0, MotionEvent arg1)
    {
        // TODO Auto-generated method stub
        return detector.onTouchEvent(arg1);
    }

    @Override
    public boolean onDown(MotionEvent e)
    {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY)
    {
        int FLING_MIN_DISTANCE = 50, FLING_MIN_VELOCITY = 200;
        // if (page == 2) {
        if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
            && Math.abs(velocityX) > FLING_MIN_VELOCITY)
        {
            // Fling right
            Log.i("page：" + page + "MyGesture", "Fling right");
            finish();
        }
        // }
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY)
    {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e)
    {
        // TODO Auto-generated method stub
        return false;
    }

}
